import emc
import sys
import hal
import glib
import time

debug = 1

class HandlerClass:
	text = ""
	capital = 0
	shift = 0
	'''
	class with gladevcp callback handlers
	'''
	def __init__(self, halcomp,builder,useropts):
		'''
		Handler classes are instantiated in the following state:
		- the widget tree is created, but not yet realized (no toplevel window.show() executed yet)
		- the halcomp HAL component is set up and the widhget tree's HAL pins have already been added to it
		- it is safe to add more hal pins because halcomp.ready() has not yet been called at this point.
		
		after all handlers are instantiated in command line and get_handlers() order, callbacks will be
		connected with connect_signals()/signal_autoconnect()
		The builder may be either of libglade or GtkBuilder type depending on the glade file format.
		'''
		
		self.halcomp = halcomp
		self.builder = builder
		self.command = emc.command();
		self.stat = emc.stat();
		self.speedspinner = self.builder.get_object('speedspinner')
		self.command.state(emc.STATE_ESTOP_RESET)
		self.command.state(emc.STATE_ON)
		self.update_pos()
		'''inifile = emc.ini(sys.argv[2])
		self.jog_speed = (
			inifile.find("DISPLAY", "DEFAULT_LINEAR_VELOCITY")
			or inifile.find("TRAJ", "DEFAULT_LINEAR_VELOCITY")
			or inifile.find("TRAJ", "DEFAULT_VELOCITY")
			or 1.0)
		'''
	def on_leftarrow_pressed(self,widget,data=None):
		print "LeftArrow"
		self.command.jog(emc.JOG_CONTINUOUS, 0, -self.speedspinner.get_value())
		self.update_pos()
	def on_rightarrow_pressed(self,widget,data=None):
		print "RightArrow"
		self.command.jog(emc.JOG_CONTINUOUS, 0, self.speedspinner.get_value())
		self.update_pos()
	def on_uparrow_pressed(self,widget,data=None):
		print "UpArrow"
		self.command.jog(emc.JOG_CONTINUOUS, 1, self.speedspinner.get_value())
		self.update_pos()
	def on_downarrow_pressed(self,widget,data=None):
		print "DownArrow"
		self.command.jog(emc.JOG_CONTINUOUS, 1, -self.speedspinner.get_value())
		self.update_pos()
	def jog_stop(self,widget,data=None):
		self.command.jog(emc.JOG_STOP, 0)
		self.command.jog(emc.JOG_STOP, 1)
		self.update_pos()
	def update_pos(self):
		self.stat.poll()
		p = self.stat.actual_position
		x = p[0] - self.stat.g5x_offset[0] - self.stat.tool_offset[0]
		y = p[1] - self.stat.g5x_offset[1] - self.stat.tool_offset[1]
		z = p[2] - self.stat.g5x_offset[2] - self.stat.tool_offset[2]
		x -= self.stat.g92_offset[0]
		y -= self.stat.g92_offset[1]
		z -= self.stat.g92_offset[2] 
		self.builder.get_object('xa').set_label("%10.4f" % x)
		self.builder.get_object('ya').set_label("%10.4f" % y)
		self.builder.get_object('za').set_label("%10.4f" % z)

def get_handlers(halcomp,builder,useropts):
	'''
	this function is called by gladevcp at import time (when this module is passed with '-u <modname>.py')
	
	return a list of object instances whose methods should be connected as callback handlers
	any method whose name does not begin with an underscore ('_') is a  callback candidate
	
	the 'get_handlers' name is reserved - gladevcp expects it, so do not change
	'''
	return [HandlerClass(halcomp,builder,useropts)]
